home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: Creating a pointer to a function "void (*ptrFunction)()" inside a class
- Date: 5 Jan 1996 23:06:51 +1100
- Organization: Werple Internet, Melbourne
- Message-ID: <4cj48r$kq0@werple.net.au>
- References: <30ECA10F.3D99@ifu.net> <4civ47$hg6@werple.net.au>
- NNTP-Posting-Host: werple.mira.net.au
-
- davidw@werple.net.au (David White) writes:
-
-
- > class BaseClass
- > {
- > protected:
- > // A typedef simplifies things somewhat.
- > // Also, the correct function parameters (int, int) are required
- > typedef int (BaseClass::*FPTR)(int, int);
- > FPTR ptrFunction;
- > };
-
- > class DerivedClass : BaseClass
- > {
- > public:
- > DerivedClass()
- > { ptrFunction = (FPTR)&DerivedClass::myFunction;
- > }
- > int myFunction(int, int);
- > }
-
- I should add a warning about this code. In some circumstances the value of
- 'this' is not the same for the base class and the derived class. For
- example, using my compiler, a difference in values would occur if I added
- a virtual function to DerivedClass, and there are no virtual functions in
- BaseClass. So, because the function pointer is declared as pointing to a
- BaseClass function, 'myFunction' will not receive the correct DerivedClass
- 'this' pointer. The problem can be corrected, on my compiler at least, by
- ensuring that BaseClass has at least one virtual function if any of its
- derived classes do also, but I don't know if you can count on that for
- other compilers. Multiple inheritance can also cause this problem.
-
- David White
- davidw@werple.mira.net.au
-